package ll.sms.dao;

import java.util.ArrayList;
import java.util.List;

import ll.sms.db.SqlBuilder;
import ll.sms.exceptions.SmsDBException;
import ll.sms.pojo.SmsAdmin;
import ll.sms.util.SmsUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class SmsAdminDAO extends BaseDAO {
	private static Log log = LogFactory.getLog(SmsAdminDAO.class);

	public void saveAdmin(SmsAdmin admin) {
		// aid,auname, apwd,acompany,aregisttime, arank, aavailable
		//aaddress,aphone,alinkman,alinkmanphone,alinkmanqq
		try {
			String sql = SqlBuilder.getSql("SAVE_ADMIN",
					admin.getAid(),
					admin.getAuname(),
					admin.getApwd(),
					admin.getAcompany(),
					admin.getAregisttime(),
					admin.getArank(),
					admin.getAavailable(),// DATE
					admin.getAsmsnum(),
					admin.getAdefaultchannel(),
					admin.getAaddress(),
					admin.getAphone(),
					admin.getAlinkman(),
					admin.getAlinkmanphone(),
					admin.getAlinkmanqq()
					);

			this.update(sql, getConnection());

		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			throw new SmsDBException(e);
		}

	}

/*	public int updatePropertyByAdminName(String fieldName, Object value,
			String adminName) {
		try {
			String sql = SqlBuilder.getSql("UPDATE_BY_admin", fieldName, value,
					uname);
			return this.update(sql, getConnection());
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			throw new SmsDBException(e);

		}

	}*/

	public int updateAdmin(SmsAdmin admin){
		 try {
			 String sql = SqlBuilder.getSql("UPDATE_ADMIN",

						admin.getAuname(),
						admin.getApwd(),
						admin.getAcompany(),
						admin.getAregisttime(),
						admin.getArank(),
						admin.getAavailable(),
						admin.getAsmsnum(),
						admin.getAdefaultchannel(),
						admin.getAaddress(),
						admin.getAphone(),
						admin.getAlinkman(),
						admin.getAlinkmanphone(),
						admin.getAlinkmanqq(),
						admin.getAid()
						);

				return this.update(sql, getConnection());
		 } catch (Exception e) {
		 e.printStackTrace();
		 log.error(e);
		 throw new SmsDBException(e);

		 }

	}

	public int updateAdminById(String fieldName,Object value,String id){
		 try {
			 String sql = SqlBuilder.getSql("UPDATE_ADMIN_BY_ID", SqlBuilder.toPropertyString(fieldName),
						value,id);
				return this.update(sql, getConnection());
		 } catch (Exception e) {
		 e.printStackTrace();
		 log.error(e);
		 throw new SmsDBException(e);

		 }

	}


	public List<SmsAdmin> findAdminByProperties(String fieldName, Object value) {
		try {
			String sql = SqlBuilder.getSql("QUERY_ADMIN_BY_PROPERTY", SqlBuilder.toPropertyString(fieldName),
					value);

			ArrayList<SmsAdmin> list = this.searchToBeanList(getConnection(),
					SmsAdmin.class, sql);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			throw new SmsDBException(e);

		}
	}

	public SmsAdmin findAdminByIdOrUname(String fieldName, String value){
		List<SmsAdmin> list=findAdminByProperties(fieldName, value);
		return (list==null || list.size()==0)?null:list.get(0);
	}


	public int deleteadminByProperty(String fieldName, Object value){
		 try {
			 String sql = SqlBuilder.getSql("DELETE_ADMIN_BY_PROPERTY", SqlBuilder.toPropertyString(fieldName),
						value);
				return this.update(sql, getConnection());
		 } catch (Exception e) {
		 e.printStackTrace();
		 log.error(e);
		 throw new SmsDBException(e);

		 }

	}

	public List<SmsAdmin> findAllAdmins(){
		 try {
			 String sql = SqlBuilder.getSql("QUERY_ALL_ADMIN");
			 return this.searchToBeanList(getConnection(), SmsAdmin.class, sql);
		 } catch (Exception e) {
		 e.printStackTrace();
		 log.error(e);
		 throw new SmsDBException(e);

		 }
	}

	public int countByUname(String username){
		 try {
			 String sql = "select count(aid) from sms_admin where auname= '"+username+"'";
			Object[] a =  this.searchToArray(sql, getConnection());;
			if(a==null || a.length!=1){
				return -1;
			}
			return SmsUtil.toInt(a[0]);

		 } catch (Exception e) {
		 e.printStackTrace();
		 log.error(e);
		 throw new SmsDBException(e);

		 }
	}

	public int updateAccountByUname(String uname,long value){
		 try {
			 String sql = SqlBuilder.getSql("UPDATE_ADMIN_ACCOUNT",value*-1,uname);
			 return this.update(sql, getConnection());
		 } catch (Exception e) {
		 e.printStackTrace();
		 log.error(e);
		 throw new SmsDBException(e);

		 }
	}


	public int updatePassword(String id ,String newPassword){
		 try {
			 String sql = SqlBuilder.getSql("UPDATE_ADMIN_PASSWORD",newPassword,id);
			 return this.update(sql, getConnection());

		 } catch (Exception e) {
		 e.printStackTrace();
		 log.error(e);
		 throw new SmsDBException(e);

		 }
	}

	public int setAdminDefaultChannel(String channelId){
		 try {
			 String sql = SqlBuilder.getSql("UPDATE_ADMIN_CHANNEL",channelId);
			 return this.update(sql, getConnection());
		 } catch (Exception e) {
		 e.printStackTrace();
		 log.error(e);
		 throw new SmsDBException(e);

		 }
	}
	public static void main(String[] args) {
		//for (int i = 0; i < 21; i++) {

/*
		SmsAdmin admin = new SmsAdmin();
		admin.setAuname("admin"+i);
		String s = SmsUtil.getPrimaryKey();
		System.out.println(s);
		admin.setAid(s);
		admin.setApwd("123456"+i);
		admin.setAcompany("creator"+i);
		admin.setAsmsnum(100);
		admin.setAregisttime(new Date());*/
		try {
			ll.sms.db.Transaction.begin();
			//new SmsAdminDAO().saveAdmin(admin);
			//new SmsAdminDAO().deleteadminByProperty("auname", "admin1");
			//List l = new SmsAdminDAO().findAdminByProperties("auname","123");
			new SmsAdminDAO().updateAccountByUname("admin", 12);
			ll.sms.db.Transaction.commit();
			//System.out.println(l.get(0));
		} catch (Exception e) {
			e.printStackTrace();
		}
		//}

	}
}
